import { CdtMetaData, Role } from '@/types/role';
import { create } from 'zustand';
import { getItem, removeItem, setItem } from '@/utils/storage';
import { StorageEnum } from '@/types/enum';
import userService from '@/api/services/userService';
import roleService from '@/api/services/roleService';
import { findParentIdArray } from '@/utils/tree';

type RoleManageType = {
  selectedRole: Role | undefined;
  expandItems: Record<string, boolean>;
  isEnableRoleConditions: boolean;
};
type RoleManageStore = {
  roleManage: RoleManageType;
  roles: Role[];
  cdtMetaData: CdtMetaData;
  // 使用 actions 命名空间来存放所有的 action
  actions: {
    setRoleManage: (roleManage: RoleManageType) => void;
    setSelectedRole: (role: Role | undefined) => void;
    setExpandItems: (expandItems: Record<string, boolean>) => void;
    setRoles: (roles: Role[]) => void;
    getRoles: () => void;
    getCdtMetaData: ()=> void;
    clearRoleManage: () => void;
    setEnableRoleConditions: (isEnableRoleConditions: boolean) => void;
    getRoleParentArray: () => string[];
  };
};

const useRoleManageStore = create<RoleManageStore>((set, get) => ({
  roleManage: getItem<RoleManageType>(StorageEnum.RoleManage) || {
    selectedRole: undefined,
    expandItems: {},
    isEnableRoleConditions: false,
  },
  roles: [],
  cdtMetaData: { params_description: "", condition_description: "", ops: [], params: [] },
  actions: {
    setRoleManage: (roleManage) => {
      set({ roleManage });
      setItem(StorageEnum.RoleManage, roleManage);
    },
    setRoles: (roles) => {
      set({ roles });
    },
    async getRoles() {
      const roles = await userService.getRole();
      set({ roles });
    },
    async getCdtMetaData() {
      const cdtMetaData = await roleService.getConditionMetaData();
      set({ cdtMetaData });
    },
    setSelectedRole(selectedRole) {
      const currentRoleManage = get().roleManage;
      const updatedRoleManage = { ...currentRoleManage, selectedRole };
      get().actions.setRoleManage(updatedRoleManage);
    },
    setExpandItems(expandItems) {
      const currentRoleManage = get().roleManage;
      const updatedRoleManage = { ...currentRoleManage, expandItems };
      get().actions.setRoleManage(updatedRoleManage);
    },
    clearRoleManage() {
      removeItem(StorageEnum.RoleManage);
    },
    setEnableRoleConditions(isEnableRoleConditions) {
      const currentRoleManage = get().roleManage;
      const updatedRoleManage = { ...currentRoleManage, isEnableRoleConditions };
      get().actions.setRoleManage(updatedRoleManage);
    },
    getRoleParentArray(){
      const roles = get().roles;
      const selectedRole = get().roleManage.selectedRole;
      const array = findParentIdArray(roles, selectedRole?.id ?? "");
      return array || []
    }
  },
}));
export const useRoleManage = () => useRoleManageStore((state) => state.roleManage);
export const useRoles = () => useRoleManageStore((state) => state.roles);
export const useRoleState = () => useRoleManageStore((state) => state);
export const useRoleManageActions = () => useRoleManageStore((state) => state.actions);
